#include "server_unit.h"
#include "timer.h"

DEFINE_string(attachment, "", "Carry this along with requests");
DECLARE_string(protocol);
DECLARE_string(connection_type);
DECLARE_int32(timeout_ms);
DEFINE_int32(port, 31010, "server brpc port");
// DEFINE_int32(port, 31111, "server brpc port");
DEFINE_string(listen_addr, "",
              "Server listen address, may be IPV4/IPV6/UDS."
              " If this is set, the flag port will be ignored");
// DEFINE_string(listen_addr, "localhost:31111",
//               "Server listen address, may be IPV4/IPV6/UDS."
//               " If this is set, the flag port will be ignored");
DEFINE_int32(brpc_thread_num, 16, "server brpc thread_num");
DEFINE_string(brpc_service_name, "grpc_mock_dpu_server", "service name");
DEFINE_int32(idle_timeout_s, -1,
             "Connection will be closed if there is no "
             "read/write operations during the last `idle_timeout_s'");
DECLARE_string(load_balancer);

DEFINE_int32(batch_size, 1, "batch_size");
DEFINE_int32(feature_num, 1, "feature_num");
DEFINE_int32(ele_num, 1, "ele_num per feature");
#if defined(__aarch64__)
DEFINE_string(dispatch_device_type, "arm", "which device to dispatch");
#else
DEFINE_string(dispatch_device_type, "cpu", "which device to dispatch");
#endif

#if defined(__aarch64__)
DEFINE_string(dag_filename, "/home/ubuntu/xeg/BenchBF3/rap/data/preprocess_ir.txt", "dag extract from");
#else
DEFINE_string(dag_filename, "/root/xeg/BenchBF3/rap/data/preprocess_ir.txt", "dag extract from");
#endif

DEFINE_int32(if_pipeline, 0, "if is pipeline cross device or tuopu");

int main(int argc, char **argv) {

    // Parse gflags. We recommend you to use gflags as well.
    // GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
    gflags::ParseCommandLineFlags(&argc, &argv, true);

    // Generally you only need one Server.
    brpc::Server server;

    rap::DocaDmaServiceImpl service_impl;
    service_impl.batch_num = FLAGS_batch_size;
    service_impl.feature_num = FLAGS_feature_num;
    service_impl.ele_num = FLAGS_ele_num;
    service_impl.filename = FLAGS_dag_filename;
    service_impl.if_pipeline = FLAGS_if_pipeline;

    xmh::Reporter::StartReportThread();
    
    if (server.AddService(&service_impl, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
        LOG(ERROR) << "Fail to add service";
        return -1;
    }
    butil::EndPoint point;
    if (!FLAGS_listen_addr.empty()) {
        // if (butil::str2endpoint(FLAGS_listen_addr.c_str(), &point) < 0) {
        if (butil::str2endpoint("127.0.0.1:31111", &point) < 0) {
            LOG(ERROR) << "Invalid listen address:" << FLAGS_listen_addr;
            return -1;
        }
    } else {
        point = butil::EndPoint(butil::IP_ANY, FLAGS_port);
    }
    // Start the server.
    brpc::ServerOptions options;
    options.idle_timeout_sec = FLAGS_idle_timeout_s;
    if (server.Start(point, &options) != 0) {
        LOG(ERROR) << "Fail to start EchoServer";
        return -1;
    }

    // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
    server.RunUntilAskedToQuit();

    xmh::Reporter::StopReportThread();
    xmh::Reporter();

    return 0;
}